//
//  PieChartLayer.m
//  carcareIOS
//
//  Created by ileo on 15/7/8.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "PieChartLayer.h"

#define GAP 0

//in [0..1], out [0..1]
static inline float easeInOut(float x){
    //1/(1+e^((0.5-x)*12))
    return 1/(1+powf(M_E, (0.5-x)*12));
}

@implementation PieModel

@end

@interface PieChartLayer()

@property (nonatomic, copy) NSArray *models;

@end

@implementation PieChartLayer

-(instancetype)init{
    self = [super init];
    if (self) {
        self.contentsScale = [[UIScreen mainScreen] scale];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.contentsScale = [[UIScreen mainScreen] scale];
    }
    return self;
}

-(void)updateValueModels:(NSArray *)models{
    self.models = models;
    [self setNeedsDisplay];
}

#pragma mark -
-(UIFont *)font{
    if (!_font) {
        _font = [UIFont systemFontOfSize:15];
    }
    return _font;
}

-(void)drawInContext:(CGContextRef)ctx{
    if (self.models.count == 0) {
        return;
    }
    CGFloat sum = 0;
    for (int i = 0; i < self.models.count; i++) {
        PieModel *model = self.models[i];
        sum += model.value;
    }
    if (sum <= 0) {
        return;
    }
    
    CGPoint center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
    
    CGFloat angleStart = self.startAngle * M_PI / 180;
    CGFloat angleInrerVal = 2 * M_PI;
    
    for (int i = 0; i < self.models.count; i++) {
        PieModel *model = self.models[i];
        CGFloat angleEnd = angleStart + angleInrerVal * model.value / sum;
        [self drawPieChartWithAngleStart:angleStart center:center angleEnd:angleEnd ctx:ctx model:model];
        
        [self drawTextWithAngleEnd:angleEnd angleStart:angleStart model:model center:center ctx:ctx];
        
        angleStart = angleEnd;
    }
    
}

- (void)drawPieChartWithAngleStart:(CGFloat)angleStart center:(CGPoint)center angleEnd:(CGFloat)angleEnd ctx:(CGContextRef)ctx model:(PieModel *)model {
    CGPoint minRadiusStart = CGPointMake(center.x + self.minRadius * cosf(angleStart), center.y + self.minRadius * sinf(angleStart));
    CGPoint maxRadiusEnd = CGPointMake(center.x + self.maxRadius * cosf(angleEnd), center.y + self.self.maxRadius * sinf(angleEnd));
    
    CGContextSaveGState(ctx);
    
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, minRadiusStart.x, minRadiusStart.y);
    CGContextAddArc(ctx, center.x, center.y, self.minRadius, angleStart, angleEnd, NO);
    CGContextAddLineToPoint(ctx, maxRadiusEnd.x, maxRadiusEnd.y);
    CGContextAddArc(ctx, center.x, center.y, self.maxRadius, angleEnd, angleStart, YES);
    CGContextClosePath(ctx);
    CGContextClip(ctx);
    
    CGContextSetFillColorWithColor(ctx, model.color.CGColor);
    CGContextFillRect(ctx, self.bounds);
    
    CGContextRestoreGState(ctx);
}

- (void)drawTextWithAngleEnd:(CGFloat)angleEnd angleStart:(CGFloat)angleStart model:(PieModel *)model center:(CGPoint)center ctx:(CGContextRef)ctx {
    
    CGFloat angle = (angleStart + angleEnd) / 2.0;
    while (angle < -M_PI_4) {
        angle += M_PI*2;
    }
    while (angle >= 2*M_PI - M_PI_4) {
        angle -= M_PI*2;
    }
    CGSize textSize = [model.title sizeWithAttributes:@{NSFontAttributeName:self.font}];
    CGPoint anchorPoint;
    //clockwise
    if(angle >= -M_PI_4 && angle < M_PI_4){
        anchorPoint = CGPointMake(0, easeInOut((M_PI_4-angle) / M_PI_2));
    } else if(angle >= M_PI_4 && angle < M_PI_2+M_PI_4){
        anchorPoint = CGPointMake(easeInOut((angle-M_PI_4) / M_PI_2), 0);
    } else if(angle >= M_PI_2+M_PI_4 && angle < M_PI+M_PI_4){
        anchorPoint = CGPointMake(1, easeInOut((angle - (M_PI_2+M_PI_4)) / M_PI_2));
    } else {
        anchorPoint = CGPointMake(easeInOut(((2*M_PI - M_PI_4) - angle) / M_PI_2), 1);
    }
    CGPoint pos = CGPointMake(center.x + (self.maxRadius + GAP) * cosf(angle), center.y + (self.maxRadius + GAP) * sinf(angle));
    CGRect frame = CGRectMake(pos.x - anchorPoint.x * textSize.width,
                              pos.y - anchorPoint.y * textSize.height,
                              textSize.width,
                              textSize.height);
    
    
    UIGraphicsPushContext(ctx);
    [model.title drawInRect:frame withAttributes:@{NSFontAttributeName:self.font, NSForegroundColorAttributeName : model.color}];
    UIGraphicsPopContext();
}

@end
